import Vue from "vue";
import Vuex from "vuex";
import router from '@/router'
import { getUser, getToken, getConsData } from "@/api/index";
import { EncryptSM4 } from "@/utils/allSM";
import { dataManage } from "@/utils";
import { hideLoading } from '@/utils/base'
import { ZgtToast } from "@/components/zgt-comps";
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    title: "",
    isNavbar: false,
    token: sessionStorage.getItem('appToken') || '', // 测试用
    uid: "", // 测试用
    // 录入类型
    userType: 1,
    // 个人用户信息
    personalInfo: null,
    // 企业用户信息
    corporationInfo: null,
    // 接口需要携带的参数
    params: null,

    // 状态
    stateList: [],
    // 区域
    area: [],
  },
  getters: {},
  mutations: {
    SET_TITLE: (state, data) => {
      state.title = data;
    },
    SET_IS_NAVBAR: (state, data) => {
      state.isNavbar = data;
    },
    SET_TOKEN: (state, data) => {
      if (data) {
        state.token = data;
      }
    },
    SET_UID: (state, data) => {
      state.uid = data;
    },
    SET_USER_TYPE: (state, data) => {
      state.userType = data;
    },
    SET_PERSONAL_INFO: (state, data) => {
      state.personalInfo = data;
    },
    SET_CORPORATION_INFO: (state, data) => {
      state.corporationInfo = data;
    },
    SET_PARAMS: (state, data) => {
      state.params = data;
    },
    SET_STATELIST: (state, data) => {
      state.stateList = data;
    },
    SET_AREA: (state, data) => {
      state.area = data;
    },
  },
  actions: {
    /**
     * 获取用户信息
     * @param {*} param
     */
    async getUser({ commit, dispatch, state }) {
      try {
        if (!state.token) {
          router.push('/403')
          return
        }
        const { data } = await getUser({ token: state.token });
        if (data) {
          commit("SET_TOKEN", data.data.token);
          commit("SET_USER_TYPE", data.data.userType);
          if (state.userType == '1') {
            commit("SET_UID", data.data.personalInfo.id);
            commit("SET_PERSONAL_INFO", data.data.personalInfo);
          } else {
            commit("SET_UID", data.data.corporationInfo.id);
            commit("SET_CORPORATION_INFO", data.data.corporationInfo);
          }
          dispatch("getToken");
        }
      } catch {
        ZgtToast.fail({
          duration: 3000, // 持续展示 toast
          message: '获取用户信息失败！',
        });
      }
    },

    /**
     * 获取 token
     * @param {*} param
     */
    async getToken({ commit, state }) {
      try {
        const { data } = await getToken({
          uid: EncryptSM4(state.uid),
        });
        commit("SET_PARAMS", data);

        getConsData().then((res) => {
          let area = res.data.area;
          commit("SET_AREA", dataManage(area));
          commit("SET_STATELIST", res.data.flowState);
          hideLoading()
        });
      } catch (err) {
        console.log("error", err);
        ZgtToast.fail({
          duration: 3000, // 持续展示 toast
          message: '获取token失败',
        });
      }
    },
  },
  modules: {},
});
